package com.szqhyxj.youxiangju.biz.partnerHome.shop.order;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.OrderDeetails;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 下级订单详情
 */
public class SubordinateOrderDetailsActivity extends BaseActivity implements SubordinateOrderDetailsContract.View, NavigationBar.NavigationBarListener{

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.type_title)
    TextView typeTitle;
    @BindView(R.id.sure_delivery)
    Button sureDelivery;
    @BindView(R.id.type_line)
    LinearLayout typeLine;
    @BindView(R.id.information_background)
    View informationBackground;
    @BindView(R.id.icon)
    ImageView icon;
    @BindView(R.id.commodite_title)
    TextView commoditeTitle;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.quantity)
    TextView quantity;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.user_phone)
    TextView userPhone;
    @BindView(R.id.user_name_phone)
    LinearLayout userNamePhone;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.express_id)
    TextView expressId;
    @BindView(R.id.copy)
    TextView copy;
    @BindView(R.id.express)
    LinearLayout express;
    @BindView(R.id.express_line)
    View expressLine;
    @BindView(R.id.distribution_type)
    TextView distributionType;
    @BindView(R.id.type)
    LinearLayout type;

    @BindView(R.id.the_order_price)
    TextView theOrderPrice;
    @BindView(R.id.price_l)
    LinearLayout priceL;
    @BindView(R.id.price_line)
    View priceLine;
    @BindView(R.id.payment_type_context)
    TextView paymentTypeContext;
    @BindView(R.id.payment_type)
    LinearLayout paymentType;

    @BindView(R.id.order_id_context)
    TextView orderIdContext;
    @BindView(R.id.order_id)
    LinearLayout orderId;

    @BindView(R.id.order_time_context)
    TextView orderTimeContext;
    @BindView(R.id.order_time)
    LinearLayout orderTime;

    @BindView(R.id.delivery_time_context)
    TextView deliveryTimeContext;
    @BindView(R.id.delivery_time)
    LinearLayout deliveryTime;
    @BindView(R.id.order_details)
    ScrollView orderDetails;

    private static String ORDERID = "orderid";
    @BindView(R.id.receipt_time_context)
    TextView receiptTimeContext;
    @BindView(R.id.receipt_time)
    LinearLayout receiptTime;
    @BindView(R.id.platform_type)
    TextView platformType;
    @BindView(R.id.commodite_title_type)
    LinearLayout commoditeTitleType;
    @BindView(R.id.unit_type_context)
    TextView unitTypeContext;
    @BindView(R.id.look_unit)
    TextView lookUnit;
    @BindView(R.id.unit_type)
    LinearLayout unitType;

    private SubordinateOrderDetailsPresenter presenter;
    private String orderINo;

    public static void start(Activity activity, String orderid){
        Intent intent = new Intent();
        intent.setClass(activity, SubordinateOrderDetailsActivity.class);
        intent.putExtra(ORDERID, orderid);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subordinate_order_details);
        ButterKnife.bind(this);
        navigationBar.setNavigationBarListener(this);
        presenter = new SubordinateOrderDetailsPresenter(this);
        orderINo = getIntent().getStringExtra(ORDERID);
        presenter.initOrderDetail(orderINo);
    }

    @OnClick({R.id.sure_delivery})
    public void onViewClicked(View view){
        switch(view.getId()){
            case R.id.sure_delivery:
                presenter.nowConfirm();
                break;
        }
    }

    @Override
    public void showOrderDetails(OrderDeetails orderDeetails){
                orderDetails.setVisibility(View.VISIBLE);

                if(orderDeetails.getDeliverStatus().equals("0")){  //未发货
                    typeTitle.setText("支付成功,等待发货");
                    sureDelivery.setVisibility(View.VISIBLE);
                    express.setVisibility(View.GONE);
                    receiptTime.setVisibility(View.GONE);
                    deliveryTime.setVisibility(View.GONE);
                    lookUnit.setVisibility(View.GONE);
                    type.setVisibility(View.GONE);
                }else { //已发货
                    sureDelivery.setVisibility(View.GONE);
                    if(orderDeetails.getReceiverStatus().equals("1")){ //已经接收
                        typeTitle.setText("已确定收货,订单完成");
                        receiptTimeContext.setText(orderDeetails.getDeliverTime());
                    }else {
                        typeTitle.setText("已确定发货，等待收货");
                        receiptTime.setVisibility(View.GONE);
                    }
                }

                LoadImageUtils.loadImage(icon, orderDeetails.getFile());
                commoditeTitle.setText(orderDeetails.getPackageName());

                price.setText(StringUtils.initPrice(orderDeetails.getTotalPrice()));
                quantity.setText("/" + orderDeetails.getTotalNum() + "台");
                userName.setText(orderDeetails.getUsername());
                userPhone.setText(orderDeetails.getPhone());
                address.setText(orderDeetails.getAddress());
                expressId.setText(orderDeetails.getTrackingNumber());
                unitTypeContext.setText(orderDeetails.getBrandSubName());

                if(TextUtils.isEmpty(orderDeetails.getDistributionType())){
                    type.setVisibility(View.GONE);
                }else {
                    if(orderDeetails.getDistributionType().equals("1")){ //1=当面配送，2=物流配送
                        distributionType.setText("当面配送");
                    }else{
                        distributionType.setText("物流配送");
                    }
                }

                theOrderPrice.setText(StringUtils.initPrice(orderDeetails.getTotalPrice())+"元");
                orderIdContext.setText(orderDeetails.getPayOrderId());
                orderTimeContext.setText(orderDeetails.getAddTime());
                deliveryTimeContext.setText(orderDeetails.getDeliverTime());

    }

    @Override
    public void navConfirmOrder(){
        DeliveryOrderActivity.start(this, orderINo);
    }

    @Override
    public void navigationLeft(){
        finish();
    }

    @Override
    public void navigationRight(){
    }


}
